package cn.kioye.secretbmp.test;

import cn.kioye.secretbmp.utils.BmpUtil;
import cn.kioye.secretbmp.utils.BmpUtil.Position;
import cn.kioye.secretbmp.utils.ByteUtil;

import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

import javax.imageio.ImageIO;

public class testMain {

    /**
     * 方法体入口
     * InjectBmp(fileBmp,fileSecret)--->注入隐藏数据
     * SuckBmp(fileBmpou,fileRecovery)--->读出隐藏数据
     * @param args
     */
    public static void main(String[] args) {
        File fileBmp = new File("src/main/resources/origin.bmp");
        File fileSecret = new File("src/main/resources/origin.txt");
        //编译后路径
        //URL resource = Class.class.getClass().getResource("/origin.bmp");

        File fileBmpou=new File("src/main/resources/secret.bmp");
        File fileRecovery=new File("src/main/resources/secret.txt");

        try {
            //注入方法调用
			new testMain().InjectBmp(fileBmp,fileSecret);

            //提取方法调用
            byte[] result = new testMain().SuckBmp(fileBmpou,fileRecovery);
            System.out.println(new String(result, "UTF-8"));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 将文本信息从BMP图片取出
     *
     * @param fileBmp 存有隐藏数据的BMP图片路径
     * @param fileSecret 取出数据到 [可空]
     * @return 取出数据Bytes
     * @throws Exception
     */
    public byte[] SuckBmp(File fileBmp, File fileSecret) throws Exception {
        FileOutputStream ou = null;

        if (fileSecret != null) {
            if (fileSecret.exists()) {
                fileSecret.delete();
            }
            ou = new FileOutputStream(fileSecret);
        }
        BufferedImage bi = ImageIO.read(fileBmp);
        int width = bi.getWidth();
        int height = bi.getHeight();
        System.out.println("bmp:" + width + "*" + height + "=" + (width * height));
        //读取文件大小信息
        byte[] tranRead = BmpUtil.read(bi, new Position(), 8);
        long total = ByteUtil.getLong(tranRead, 0);

        //除去文件大小信息（8字节）的起点
        Position position = new Position((22) % bi.getWidth(), 22 / bi.getWidth(), 1);

        int len = 1024;
        System.out.println(total);
        byte[] allbt = null;
        while ((total - 1) / len > 1 || (len = (int) total) > 0) {
            total -= len;
            byte[] tranRead2 = BmpUtil.read(bi, position, len);
            allbt = (allbt == null) ? tranRead2 : ByteUtil.addBytes(allbt, tranRead2);
            if (ou != null) {
                ou.write(tranRead2);
                ou.flush();
            }
        }
        if (ou != null) {
            ou.close();
        }
        return allbt;
    }


    
    /**
     * 将文本信息注入BMP图片
     * 原理:图片一个像素[3原色(RGB)，即3字节]可分离出3位，8像素可分离3字节
     *
     * @param fileBmp 要存隐藏数据的BMP图片路径
     * @param fileSecret 要存的隐藏数据路径
     * @throws Exception
     */
    public void InjectBmp(File fileBmp, File fileSecret) throws Exception {
        FileInputStream in = new FileInputStream(fileSecret);
        BufferedImage bi = ImageIO.read(fileBmp);

        int inSize = 1024;

        byte[] inbt = new byte[inSize];

        //8个字节所占像素约为22
        int pixel=8*8/3+(8*8%3==0?0:1);

        Position position = new Position(pixel % bi.getWidth(), pixel / bi.getWidth(), 1);

        long len = 0;
        while ((inSize = in.read(inbt)) != -1) {
            len += inSize;
            //每次注入inSize=1024个字节，并返回下一个像素位置[具体到3原色]
            position = BmpUtil.inject(bi, position, inbt, inSize);
        }
        // 将文本大小信息保存在图片头部(8字节[充当long类型]约占22个像素)
        byte[] end = new byte[8];
        ByteUtil.putLong(end, len, 0);

        System.out.println("total:" + len + " px" + position.x + ",py" + position.y);

        BmpUtil.inject(bi, new Position(), end, 8);
        String format = "bmp";
        File fileout = new File(fileBmp.getAbsolutePath().substring(0, fileBmp.getAbsolutePath().lastIndexOf(File.separator)), "secret." + format);
        ImageIO.write(bi, format, fileout);

    }

}
